﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml;
using Scommesse.Model;
using Scommesse.Utility;

namespace Scommesse.Persistence
{
    internal static class DataSaver
    {
        public static void SavePalinsestoOnFile(Palinsesto palinsesto)
        {
            XmlWriter writer = XmlWriter.Create("palinsesto.pal");
            writer.WriteStartDocument();
            writer.WriteStartElement("Palinsesto");
            writer.WriteStartElement("Categorie");
            foreach (Categoria cat in palinsesto.Categorie)
            {
                SaveXmlCategoria(writer, cat);
            }
            writer.WriteEndElement(); //categorie
            writer.WriteEndElement(); //palinsesto
            writer.WriteEndDocument();
            writer.Close();
        }

        private static void SaveXmlCategoria(XmlWriter writer, Categoria cat)
        {
            writer.WriteStartElement("categoria");
            writer.WriteAttributeString("nome", cat.Nome);

            writer.WriteStartElement("Tipologie");
            foreach (KeyValuePair<Tipologia, Type> tipologia in cat.Tipologie)
            {
                writer.WriteStartElement("tipologia");
                writer.WriteAttributeString("nome", StringValueAttribute.GetStringValue(tipologia.Key));
                writer.WriteAttributeString("tipo", tipologia.Value.ToString());
                writer.WriteEndElement();
            }
            writer.WriteEndElement(); // Tipologie

            writer.WriteStartElement("Competizioni");
            foreach (Competizione comp in cat.Competizioni)
            {
                SaveXmlCompetizione(writer, comp);
            }
            writer.WriteEndElement(); // Competizioni

            writer.WriteEndElement(); // categoria
        }

        private static void SaveXmlCompetizione(XmlWriter writer, Competizione comp)
        {
            writer.WriteStartElement("competizione");
            writer.WriteAttributeString("nome", comp.Nome);
            writer.WriteStartElement("Eventi");

            foreach (Evento ev in comp.Eventi)
            {
                SaveXmlEvento(writer, ev);
            }

            writer.WriteEndElement();
            writer.WriteEndElement();
        }

        private static void SaveXmlEvento(XmlWriter writer, Evento ev)
        {
            writer.WriteStartElement("evento");
            writer.WriteAttributeString("id", ev.Id.ToString());
            writer.WriteAttributeString("competitoreA", ev.CompetitoreA);
            writer.WriteAttributeString("competitoreB", ev.CompetitoreB);
            writer.WriteAttributeString("dataOra", String.Format("{0:dd/MM/yyyy HH:mm}", ev.DataOra));
            writer.WriteAttributeString("stato", ev.Stato.ToString());
            if (ev.Stato.Equals(StatoEvento.Terminato))
            {
                writer.WriteAttributeString("punteggioA", ev.PunteggioA.ToString(CultureInfo.InvariantCulture));
                writer.WriteAttributeString("punteggioB", ev.PunteggioB.ToString(CultureInfo.InvariantCulture));
            }
            writer.WriteEndElement(); //evento
        }

        public static void SaveBetCardOnFile(IBetCard betCard)
        {
            FileStream fs = new FileStream(betCard.CodiceTessera + ".betCard", FileMode.OpenOrCreate);
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(fs, betCard);
            fs.Close();
        }
    }
}